<?php
namespace App\Models;

use App\Models\Model;
use App\Models\System\Term;
use App\Models\System\User;
use Carbon\Carbon;

class Teachers extends Model
{
    protected $table = 'teachers';
    protected $guarded = [];
    protected $hidden = [
        'created_at',
        'updated_at',
        'creator_id',
        'updater_id',
    ];
    protected $appends = [
        'age',
//        'id_card_no',
        'teaching_year',
        'work_year',
        'gender_set',
        'nation_set',
        'political_set',
        'native_place_set',
        'major_subject_array',
        // 'cur_assessment'
    ];

    public function user()
    {
        return $this->hasOne(User::class, 'eduid', 'eduid');
    }

    public function term()
    {
        return $this->hasOne(Term::class, 'id', 'data_version_id');
    }

    public function address()
    {
        return $this->hasOne(BasicInfoAddress::class, 'user_id', 'user_id')->where('deleted', 0);
    }

    public function assessment()
    {
        return $this->hasMany(BasicInfoAnnualAssessment::class, 'user_id', 'user_id')->where('deleted', 0)->orderBy('year', 'desc');
    }

    public function graduatedSchool()
    {
        return $this->hasOne(BasicInfoGraduatedSchool::class, 'user_id', 'user_id')->where('deleted', 0);
    }

    public function learnHistory()
    {
        return $this->hasMany(BasicInfoLearnHistory::class, 'user_id', 'user_id')->where('deleted', 0)->orderBy('begin_time', 'desc');
    }

    public function position()
    {
        return $this->hasMany(BasicInfoPosition::class, 'user_id', 'user_id')->where('deleted', 0)->orderBy('begin_date', 'desc');
    }

    public function workHistory()
    {
        return $this->hasMany(BasicInfoWorkHistory::class, 'user_id', 'user_id')->where('deleted', 0)->orderBy('begin_time', 'desc');
    }

    public function titleInfo()
    {
        return $this->hasOne(BasicTitleInfo::class, 'user_id', 'user_id')->where('deleted', 0);
    }

    public function change()
    {
        return $this->hasMany(BasicInfoChange::class, 'user_id', 'user_id');
    }

    public function getIdCardNoAttribute()
    {
        $idCard = $this->attributes['id_no'] ? $this->attributes['id_no'] : '';

        if(!$idCard) {
            return '';
        }

        return substr_replace($idCard, str_repeat("*",6), 3, 11);
    }

    public function getAgeAttribute(){
        $result = 0;

        $birthday = !empty($this->attributes['birthday']) ? $this->attributes['birthday'] : '';
        if(!$birthday){
            return $result;
        }

        $result = Carbon::parse($birthday)->age;

        return $result;
    }

    public function getTeachingYearAttribute(){
        $result = 0;

        $teachingDate = !empty($this->attributes['teaching_date']) ? $this->attributes['teaching_date'] : '';

        if(!$teachingDate){
            return $result;
        }

        $result = Carbon::parse($teachingDate)->age;

        return $result;
    }

    public function getWorkYearAttribute(){
        $result = 0;

        $workDate = !empty($this->attributes['work_date']) ? $this->attributes['work_date'] : '';
        if(!$workDate){
            return $result;
        }
        $result = Carbon::parse($workDate)->age;

        return $result;
    }



    public function getGenderSetAttribute(){
        return $this->getFieldValue('gender');
    }

    public function getNationSetAttribute(){
        return $this->getFieldValue('nation');
    }

    public function getPoliticalSetAttribute(){
        return $this->getFieldValue('political');
    }

    public function getNativePlaceSetAttribute(){
        return $this->getFieldValue('native_place');
    }

    public function getMajorSubjectArrayAttribute(){
        return $this->getFieldValue('major_subject');
    }

    public function getFieldValue($field)
    {
        $f = !empty($this->attributes[$field]) ? $this->attributes[$field] : '';

        if(!$f){
            return [];
        }

        return json_decode($f, true);
    }

}

?>
